package com.lin.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.lin.utils.JdbcUtils;
import com.lin.vo.Book;
import com.lin.vo.Order;
import com.sun.mail.util.QEncoderStream;

public class BookDaoImpl implements IBookDao {

	@Override
	public List<Book> findByAllBook() {
		QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
		String sql="SELECT * FROM book WHERE isdel = ?";
		List<Book> bookLists;
		try {
			bookLists = queryRunner.query(sql, new BeanListHandler<>(Book.class),0);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return bookLists;
	}


	@Override
	public List<Book> findByTypeBook(String bookType) {
		QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
		String sql="SELECT * FROM book WHERE cid = ? AND isdel = ?";
		List<Book> bookLists;
		try {
			bookLists=queryRunner.query(sql, new BeanListHandler<>(Book.class), bookType,0);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return bookLists;
	}


	@Override
	public Book findByIdBook(String bid) {
		QueryRunner queryRunner=new QueryRunner(JdbcUtils.getDataSource());
		String sql="SELECT * FROM book WHERE bid = ?";
		Book book;
		try {
			book = queryRunner.query(sql, new BeanHandler<>(Book.class), bid);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return book;
	}




	@Override
	public void saveBook(Book book) {
		QueryRunner  queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		String sql ="INSERT INTO book VALUES(?,?,?,?,?,?,?)";
		Object [] params ={book.getBid(),book.getBname(),book.getPrice(),book.getAuthor(),book.getImage(),book.getCid(),book.getIsdel()};
		try {
			queryRunner.update(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}


	@Override
	public void update(Book book) {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		String sql ="UPDATE book SET bname = ?,price = ?,author = ?,image = ?,cid = ?,isdel = ? WHERE bid = ?";
		Object [] params={book.getBname(),book.getPrice(),book.getAuthor(),book.getImage(),book.getCid(),book.getIsdel(),book.getBid()};
		try {
			queryRunner.update(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}


	@Override
	public List<Book> findByBookState() {
		QueryRunner queryRunner = new QueryRunner(JdbcUtils.getDataSource());
		String sql="SELECT * FROM book WHERE isdel = ?";
		List<Book> bookLists;
		try {
			bookLists= queryRunner.query(sql, new BeanListHandler<>(Book.class),1);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return bookLists;
	}





}
